const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    name: '',
    sex: 'MAN',
    age: '',
    sexs: [{
      text: '先生',
      value: 'MAN',
      checked: true
    }, {
      text: '小姐',
      value: 'WOMAN',
      checked: false
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  setName: function(e) {
    this.setData({
      name: e.detail.value
    });
  },
  setPhone: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  setAge: function(e) {
    this.setData({
      age: e.detail.value
    });
  },
  radioChange: function(e) {
    this.setData({
      sex: e.detail.value
    });
  },
  save: function() {
    const that = this;
    const name = that.data.name;
    const phone = that.data.phone;
    const age = that.data.age;
    const sex = that.data.sex;
    const url = app.globalData.url;
    const openId = app.globalData.openId;
    if (name.length == 0) {
      wx.showToast({
        title: '请输入会员姓名',
        icon: 'none'
      });
      return;
    }
    if (phone.length == 0) {
      wx.showToast({
        title: '请输入会员电话',
        icon: 'none'
      });
      return;
    }
    if (age.length == 0) {
      wx.showToast({
        title: '请输入会员年龄',
        icon: 'none'
      });
      return;
    }
    wx.request({
      url: url + '/coach/addTrainee',
      data: {
        coachOpenId: openId,
        phone,
        name,
        sex,
        age,
        companyName
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: response.data.message || '保存失败',
            icon: 'none'
          });
        }
      }
    })
  }
})